package py.com.fms.com.persistence.services;

import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import com.ibatis.sqlmap.client.SqlMapClient;

import py.com.fms.com.bean.FeesToSuppliersBean;
import py.com.fms.com.bean.PaymentOrderDetBean;
import py.com.fms.com.bean.SupplyCostStockBean;
import py.com.fms.exception.CommonException;
import py.com.fms.persistence.services.FmsServices;

public class FeesToSupplierServices extends FmsServices {

	public FeesToSupplierServices(SqlMapClient client) {
		super(client);
		// TODO Auto-generated constructor stub
	}
	

	/**
	 [[[
	 [[ The following method retrieve a list of all records
	 [[ Normally used to load a grid.
	 */
	
	@SuppressWarnings("unchecked")
	public List<FeesToSuppliersBean> getAllFeesToSuppliers() throws CommonException{
		List<FeesToSuppliersBean> queryForList;
		try {
			
			//[[ queryForList is an implemented interface of iBatis ]] 
			
			queryForList = (List<FeesToSuppliersBean>)this.getConfig().queryForList("feesToSuppliers.getAllFeesToSuppliers");
			return queryForList;
			
		} catch (SQLException e) {
			throw new CommonException(e);
		}
	}
	
	public FeesToSuppliersBean getFeesToSuppliersData(Long idFeesToSuppliers) throws CommonException{
		FeesToSuppliersBean queryForObject;
		try {
			
			//[[ queryForObject is an implemented interface of iBatis ]] 
			
			queryForObject = (FeesToSuppliersBean)this.getConfig().queryForObject("feesToSuppliers.getSuppliesCostStockById", idFeesToSuppliers);
			return queryForObject;
			
		} catch (SQLException e) {
			throw new CommonException(e);
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<FeesToSuppliersBean> getFeesToSuppliersBySupplierId(Long idSupplier) throws CommonException{
		List<FeesToSuppliersBean> queryForList;
		try {
			
			//[[ queryForList is an implemented interface of iBatis ]] 
			
			queryForList = (List<FeesToSuppliersBean>)this.getConfig().queryForList("feesToSuppliers.getFeesToSuppliersBySupplierId", idSupplier);
			return queryForList;
			
		} catch (SQLException e) {
			throw new CommonException(e);
		}
	}
	
	
	/**
	 * Returns Pending Purchases. 
	 * The passed arguments are:
	 * <p>
	 * - A Long object containing the value of the supplier id
	 * - A String object containing the purchase status
	 * - A Date object containing the purchase date
	 * 
	 * @param  		the supplier id
	 * @param  		the purchase status
	 * @param  		the purchase Date
	 * 
	 */
	@SuppressWarnings("unchecked")
	public List<PaymentOrderDetBean> getFeesToSuppliersQuery(Long idEnt, Long idSupplier) throws CommonException{
		List<PaymentOrderDetBean> queryForList;
		try {
			System.out.println("SEARCH: SUPPLIER: "+idSupplier);
			System.out.println("SEARCH: ENT: "+idEnt);
			HashMap< String, Object> param = new HashMap<String, Object>();
			param.put("idEnt", idEnt);
			param.put("idSupplier", idSupplier);
			queryForList = (List<PaymentOrderDetBean>)this.getConfig().queryForList("feesToSuppliers.getFeesToSuppliersQuery", param);
			System.out.println("despues de getConfig.queryForList a getFeesToSuppliersForPayment");
			return queryForList;
		} catch (SQLException e) {
			System.out.println("en purchasesServices.error " + e.getMessage());
			throw new CommonException(e);
		}
	}
	
	
	
	/**
	 [[[
	 [[ The following method store a bean (record) in the database.
	 */
	
	public void saveNewFeesToSuppliers(FeesToSuppliersBean feesToSuppliers) throws CommonException{
		try {
			this.getConfig().startTransaction();
			this.getConfig().insert("feesToSuppliers.insFeesToSuppliers", feesToSuppliers);
			this.getConfig().commitTransaction();
		} catch (SQLException e) {
			e.printStackTrace();
			throw new CommonException(e);
		} finally {
			try {
				this.getConfig().endTransaction();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new CommonException(e);
			}
		}
	}
	
	/**
	 [[[
	 [[ The following method delete a record from the database.
	 */
	
	public void deleteFeesToSuppliersById(Long id) throws CommonException{
		try {
			HashMap<String, Object> parameters = new HashMap<String, Object>();
			parameters.put("idFeesToSuppliers", id);
			this.getConfig().startTransaction();
			this.getConfig().delete("feesToSuppliers.delFeesToSuppliersById", parameters);
			this.getConfig().commitTransaction();
		} catch (SQLException e) {
			throw new CommonException(e);
		} finally {
			try {
				this.getConfig().endTransaction();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new CommonException(e);
			}
		}
	}
	
	/**
	 [[[
	 [[ The following method update a record from a bean.
	 */
	public void updateFeesToSuppliers(FeesToSuppliersBean feesToSuppliers) throws CommonException{
		try {
			this.getConfig().startTransaction();
			this.getConfig().update("feesToSuppliers.updFeesToSuppliersById", feesToSuppliers);
			this.getConfig().commitTransaction();
		} catch (SQLException e) {
			throw new CommonException(e);
		} finally {
			try {
				this.getConfig().endTransaction();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new CommonException(e);
			}
		}
	}
	
	/**
	 [[[
	 [[ The following method retrieve a object containing the record id
	 */
	public Long getIdFeesToSuppliers(String cod) throws CommonException{
		Long queryForObject;
		try {
			
			//[[ queryForObject is an implemented interface of iBatis ]] 
			
			queryForObject = (Long)this.getConfig().queryForObject("feesToSuppliers.getIdFeesToSuppliers", cod);
			return queryForObject;
			
		} catch (SQLException e) {
			throw new CommonException(e);
		}
	}
	
	
	/**
	 [[[
	 [[ The following method retrieve a object containing the record description
	 */
	public String getDescFeesToSuppliers(Long id) throws CommonException{
		String queryForObject;
		try {
			
			//[[ queryForObject is an implemented interface of iBatis ]] 
			
			queryForObject = (String)this.getConfig().queryForObject("feesToSuppliers.getDescFeesToSuppliers", id);
			return queryForObject;
			
		} catch (SQLException e) {
			throw new CommonException(e);
		}
	}

}
